package com.controller;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.socket.SocketServer;
import com.util.FrameDto;
import com.util.FrameUtil;
import com.util.Result;
import com.util.SerialPortManager;

@RestController
public class SerialController {

	@Autowired
	SerialPortManager portManager;
	
	@Autowired
	SocketServer socketServer;
	
	@RequestMapping(value="/linkPort",produces = "application/json")
	public  Result<String> startLink(Integer restart) {
		
		Result<String> result = new Result<>();
		if(SerialPortManager.openPort!=null) {
			if(restart!=null&&restart==1) { //关闭再重启
				SerialPortManager.openPort.close();
			}else { //返回已经连接
				result.setCode(500);
				result.setMsg("连接已经占用");
				return result;
			}
			
		}
		SerialPortManager portManager = new SerialPortManager();
		ArrayList<String> ports = SerialPortManager.findPorts();
		if(ports.size()>0) {
			for (String port : ports) {
				try {
					SerialPortManager.openPort = portManager.openPort(port, 9600);
				} catch (Exception e) {
					e.printStackTrace();
					continue;
				}
				break;
			}
			
			// 添加串口监听
            if (SerialPortManager.openPort==null){
            	System.out.println("请检查与设备的连接");
            	result.setCode(500);
    			result.setMsg("请检查与设备的连接");
                return result;
            }
            portManager.addListener(SerialPortManager.openPort,result);
            result.setCode(200);
            result.setMsg("连接成功");
		}else {
			result.setCode(500);
            result.setMsg("未发现端口连接");
		}
		return result;
	}
	
	
	@RequestMapping(value = "/test",produces = "application/json")
	public Result<String> test(String order) {
		Result<String> result = new Result<String>();
		try {
			FrameDto frameDto = FrameUtil.createData(order);
			byte[] postData = FrameUtil.toPostData(frameDto);
			System.out.println(FrameUtil.bytesToHexString(postData));
			SerialPortManager.sendToPort(SerialPortManager.openPort, postData);
			LocalDateTime begin = LocalDateTime.now(); //开始计算时间
			while (SerialPortManager.readData==null) {
				//�ȴ����ݶ�ȡ
				LocalDateTime now = LocalDateTime.now();
				Duration duration = Duration.between(begin, now);
				long millis = duration.toMillis();
				if(millis>=10000) {//10秒断开
					break;
				}
			}
			if(SerialPortManager.readData==null) {
				result.setCode(500);
				result.setMsg("读取失败");
			}else {
				FrameDto fillData = FrameUtil.fillData(SerialPortManager.readData);
				SerialPortManager.readData=null;
				JSONObject json=new JSONObject();
				JSONObject jsonObject = new JSONObject();
				String parsingData = FrameUtil.ParsingData(2,fillData.getOrder(), fillData.getData(),jsonObject,json);
				System.out.println(json.toJSONString());
				result.setCode(200);
				result.setData(parsingData);
			}
		} catch (Exception e) {
			result.setCode(500);
			result.setMsg("测试失败");
			e.printStackTrace();
		}
		
		return result;
	}
	
}
